<template>
  <div class="dashboard-container">
    <div class="app-container">
      <Upload-excel :before-upload="beforeUpload" :on-success="successFn" />
    </div>
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import EmployeesEnum from '@/contant/employee'
import { importEmployee } from '@/api/employees'
// import { parseTime } from '@/utils'
// 解构
const { mapKeyPath } = EmployeesEnum
export default {
  methods: {
    beforeUpload(file) {
      if (!file.name.endsWith('.xlsx')) {
        return alert('请选择excel文件')
      }
      return true
    },
    // successFn(data) {
    //   console.log(data)
    // } 直接解构
    async  successFn({ header, results }) {
      console.log(header, results)
      // const data = results.map((item) => {
      //   return {
      //     correctionTime: item.转正日期,
      //     mobile: item.手机号,
      //     timeOfEntry: item.入职日期,
      //     username: item.姓名,
      //     workNumber: item.工号
      //   }
      // })
      console.log(results)
      const res = results.map((item) => {
        const obj = {}
        for (const key in item) {
          // obj[mapKeyPath[key]] = item[key]
          // 判断是否需要处理时间
          if (key === '入职日期' || key === '转正日期') {
            const timesteap = item[key]
            // 将excel时间转为js时间戳 首先进去一天
            const date = new Date((timesteap - 1) * 24 * 3600000)
            // 年份差了70年
            const time = parseTime(date.setFullYear(date.getFullYear() - 70), '{y}-{m}-{d}' - 70)
            obj[mapKeyPath[key]] = time
          } else {
            obj[mapKeyPath[key]] = item[key]
          }
        }
        return obj
      })
      await importEmployee(res)
      this.$message.success('导入成功')
      this.$router.go(-1)
    }
  }
}
</script>

<style>

</style>
